﻿using System.Buffers.Text;
using System.Text;
using Growatt.Component.Cls;
using Growatt.ShinebusServiceUploadPack.Service.FeishuHelper;
using Newtonsoft.Json;

namespace Growatt.ShinebusServiceUploadPack.Service.OSS.Model
{
    public class ShinebusUploadRecordList : BaseHttpClient
    {
        private ILogger<ShinebusUploadRecordList> _logger;

        public ShinebusUploadRecordList( IHttpClientFactory httpClientFactory, ILogger<ShinebusUploadRecordList> logger, string urlUploadFileFieshuUrl)
            : base(httpClientFactory, urlUploadFileFieshuUrl)
        {
            this.baseID = baseID;
            this.tablesID = tablesID;
            this.accessToken = accessToken;
            _logger = logger;
        }

        /// <summary>
        /// 查询所有飞书已经上传固件的记录上传记录
        /// </summary>
        /// <param name="viewId"></param>
        /// <returns></returns>
        public async Task<List<ShinebusUploadFileRecordItem>> SearchRecordsShinebusAsync(string urlUploadFileFieshuUrl)
        {
            var apiHelper = new FeishuAPIHelper();
            accessToken = await apiHelper.GetTenantAccessToken();
            UpdateIDs(urlUploadFileFieshuUrl);

            // 定义请求地址
            string url = $"{BaseAddress}bitable/v1/apps/{baseID}/tables/{tablesID}/records/search?page_size=499";

            // 创建请求体
            var requestBody = new { view_id = ViewId };
            string jsonBody = JsonConvert.SerializeObject(requestBody);
            var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

            // 发送 POST 请求
            string responseContent = await SendPostRequestAsync(url, content, _logger, accessToken);

            if (!string.IsNullOrEmpty(responseContent))
            {
                var rootObject = JsonConvert.DeserializeObject<ShinebusUploadFileRecordRootobject>(responseContent);
                return rootObject.data.items.ToList();
            }

            return new List<ShinebusUploadFileRecordItem>();
        }
    }

    public class ShinebusUploadFileRecordRootobject
    {
        public int code { get; set; }
        public ShinebusUploadFileRecordData data { get; set; }
        public string msg { get; set; }
    }

    public class ShinebusUploadFileRecordData
    {
        public bool has_more { get; set; }
        public ShinebusUploadFileRecordItem[] items { get; set; }
        public int total { get; set; }
    }

    public class ShinebusUploadFileRecordItem
    {
        public ShinebusUploadFileRecordFields fields { get; set; }
        public string record_id { get; set; }
    }

    public class ShinebusUploadFileRecordFields
    {
        public ShinebusUploadFileRecordSofttype[] softType { get; set; }
        public ShinebusUploadFileRecord上传文件成功的ID[] 上传文件成功的ID { get; set; }
        public ShinebusUploadFileRecord产品类型[] 产品类型 { get; set; }
        public ShinebusUploadFileRecord审核通过的云端路径[] 审核通过的云端路径 { get; set; }
        public ShinebusUploadFileRecord新的文件夹的名称[] 新的文件夹的名称 { get; set; }
        public ShinebusUploadFileRecord设备型号[] 设备型号 { get; set; }
        public ShinebusUploadFileRecord设备类型[] 设备类型 { get; set; }
        public ShinebusUploadFileRecord是否审核[] 是否审核 { get; set; }
        public ShinebusUploadFileRecord具体操作时间[] 具体操作时间 { get; set; }
    }

    public class ShinebusUploadFileRecordSofttype
    {
        public string text { get; set; }
        public string type { get; set; }
    }

    public class ShinebusUploadFileRecord上传文件成功的ID
    {
        public string text { get; set; }
        public string type { get; set; }
    }

    public class ShinebusUploadFileRecord产品类型
    {
        public string text { get; set; }
        public string type { get; set; }
    }

    public class ShinebusUploadFileRecord审核通过的云端路径
    {
        public string text { get; set; }
        public string type { get; set; }
    }

    public class ShinebusUploadFileRecord新的文件夹的名称
    {
        public string text { get; set; }
        public string type { get; set; }
    }

    public class ShinebusUploadFileRecord设备型号
    {
        public string text { get; set; }
        public string type { get; set; }
    }

    public class ShinebusUploadFileRecord设备类型
    {
        public string text { get; set; }
        public string type { get; set; }
    }

    public class ShinebusUploadFileRecord是否审核
    {
        public string text { get; set; }
        public string type { get; set; }
    }

    public class ShinebusUploadFileRecord具体操作时间
    {
        public string text { get; set; }
        public string type { get; set; }
    }
}
